package ch.ethz.fcl.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import org.opencv.core.Mat;

import ch.ethz.fcl.tools.ImageTools;

public class ViewComponent extends Component {
	private static final long serialVersionUID = 1172362130602850706L;

	private BufferedImage image = null;
	private Mat mat = null;
	protected int width, height;

	public ViewComponent() {
		this.width = 640;
		this.height = 480;
	}

	public ViewComponent(int width, int height) {
		this.width = width;
		this.height = height;
	}

	public void update(BufferedImage image) {
		this.image = image;
		this.width = image.getWidth();
		this.height = image.getHeight();
	}

	public void update(Mat mat) {
		this.mat = mat;
		this.width = mat.cols();
		this.height = mat.rows();
	}

	@Override
	public Dimension getPreferredSize() {
		return new Dimension(width, height);
	}

	@Override
	public void paint(Graphics g) {
		Graphics2D g2 = (Graphics2D) g;
		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		if (image != null)
			g.drawImage(image, 0, 0, null);

		else if (mat != null)
			g.drawImage(ImageTools.matToImage(mat), 0, 0, null);
	}
}
